package com.rpay.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author FangXiGang
 * @date 2018/5/9
 */
public class DateUtil {
    private static final String DATETIME_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd";

    public static String formatDate(Date date) {
        return format(date, DATE_DEFAULT_FORMAT);
    }

    public static String formatDateTime(Date date) {
        return format(date, DATETIME_DEFAULT_FORMAT);
    }

    public static String format(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date parse(String dateStr, String format) {
        Date dateResult = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            dateResult = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateResult;
    }

    public static Date parseDate(String dateStr) {
        return parse(dateStr, DATE_DEFAULT_FORMAT);
    }

    public static Date parseDateTime(String dateStr) {
        return parse(dateStr, DATETIME_DEFAULT_FORMAT);
    }

    /**
     * 增加、减少多少个月，当days为负数表示减去多少个月
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonths(Date date, Integer months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

    /**
     * 增加、减少多少天，当days为负数表示减去多少天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    /**
     * 增加、减少多少天，当days为负数表示减去多少天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addWeekday(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        Date tempDate = calendar.getTime();
        if (!isRestDay(tempDate)) {
            return tempDate;
        } else {
            return addDays(tempDate, 1);
        }
    }

    /**
     * 是否是周末
     *
     * @param date
     * @return
     */
    public static boolean isRestDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == 1 || day == 7) {
            return true;
        }
        return false;
    }

    /**
     * 是否是周末
     *
     * @param date
     * @return
     */
    public static boolean isRestDay(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate(date));
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == 1 || day == 7) {
            return true;
        }
        return false;
    }

    public static int getHour() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static void main(String[] args) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
    }

}
